<?php

Raise::load('core.RaiseObject');
Raise::load('core.collection.RaiseCollection');
Raise::load('core.reflection.RaiseVariable');
Raise::load('core.helper.convert.RaiseConvert');

/**
 * RaiseDomElement class
 * Represents a DOM element
 *
 * @author Sam-Mauris Yong / hellclanner at live dot com
 * @license http://www.opensource.org/licenses/bsd-license New BSD License
 * @package phpraise.core.dom
 * @since 1.2
 */
class RaiseDomElement extends RaiseObject {
    
    /**
     * The tag name
     * @var string
     */
    protected $tagName;

    /**
     * Children of the tag
     * @var array|RaiseCollection
     */
    protected $children;

    /**
     * Attributes of the tag
     * @var RaiseCollection|array
     */
    protected $attributes;

    /**
     * Create a new RaiseDomElement
     * @param string $tagName The tag name
     * @param string $value (optional) The content/innerHTML/value of the tag
     * @param array|RaiseCollection $attributes (optional) Attributes of the HTML tag
     */
    public function __construct($tagName, $value = '', $attributes = array()){
        if(!$tagName){
            throw new RaiseInvalidArgumentException('At RaiseDomElement::__construct(), $tagName is invalid. It must be a valid string.');
        }
        $this->tagName = $tagName;
        if(is_array($value) || $value instanceof RaiseCollection || ($value && is_string($value))){
            $this->children = new RaiseCollection($value);
        }else if($value === null){
            $this->children = null;
        }else{
            $this->children = new RaiseCollection();
        }
        $this->attributes = new RaiseCollection($attributes);
    }

    /**
     * Get or set the tag name of this RaiseHtmlTag
     * @param string $t (optional) The tag name to set
     * @return string
     */
    public function tagName($t = false){
        if($t !== false){
            if(!$t){
                throw new RaiseInvalidArgumentException('$tagName is invalid. It must be a valid string.');
            }
            $this->tagName = $t;
        }
        return $this->tagName;
    }

    /**
     * Get or set the children
     * @param RaiseCollection $children (optional) Set the children of this DOM element
     * @return RaiseCollection
     */
    public function children($children = null){
        if(func_num_args() == 1){
            $this->children = $children;
        }
        return $this->children;
    }

    /**
     * Get or set the attributes of
     * @param RaiseCollection $a (optional) Set the attributes of this DOM element
     * @return RaiseCollection
     */
    public function attributes($a = false){
        if($a !== false){
            $this->attributes = $a;
        }
        return $this->attributes;
    }
    
    public function value(){
        $string = '';
        foreach($this->children as $childTag){
            $string .= RaiseConvert::to(RaiseConvert::TO_STRING, $childTag);
        }
        return $string;
    }

    /**
     * To String
     * @return string
     */
    public function __toString(){
        if($this->tagName){
            $s = '<' . $this->tagName . '';

            foreach($this->attributes as $k => $v){
                if(RaiseVariable::isType($k, RaiseVariable::TYPESTRING)){
                    $s .= ' ' . $k ;
                    $s .= '="' . str_replace(array('&', '&amp;amp;', '\\"', '"'),array('&amp;', '&amp;', '"', '\\"'), $v) . '"';
                }
            }

            if($this->children === null){
                $s .= ' />';
            }else{
                $s .= '>'; 
                $s .= $this->value();
                $s .= '</' . $this->tagName . '>';
            }
            return $s;
        }
        return 'RaiseHtmlTag';
    }    
    
}
